package com.yxlgateway.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxlgateway.filter.WordFilter;
import com.yxlcommon.common.mapper.UserMapper;
import com.yxlcommon.common.mapper.WorkCommentMapper;
import com.yxlcommon.common.mapper.WorksInfoMapper;
import com.yxlcommon.common.pojo.RespBean;
import com.yxlcommon.common.pojo.User;
import com.yxlcommon.common.pojo.WorkComment;
import com.yxlgateway.service.WorkCommentService;
import com.yxlcommon.common.vo.WorkCommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxl
 * @since 2021-11-07
 */
@Service
public class WorkCommentServiceImpl extends ServiceImpl<WorkCommentMapper, WorkComment> implements WorkCommentService {

    @Autowired
    private WorkCommentMapper workCommentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WorksInfoMapper worksInfoMapper;

    @Override
    public List<WorkCommentVo> listCommentByWorkId(Long workId) {
        return workCommentMapper.selectCommentsByWorkId(workId);
    }

    @Override
    public RespBean addComment(WorkComment workComment) {
        workComment.setContent(WordFilter.replaceWords(workComment.getContent()));
        workComment.setCreatedTime(new Timestamp(new Date().getTime()));
        return RespBean.operationSuccess("添加成功",200,workCommentMapper.insert(workComment));
    }

    @Override
    public RespBean<IPage<WorkCommentVo>> getAllWorkCommentsByPage(Integer currentPage, Integer size, WorkComment workComment) {
        Page<WorkCommentVo> page = new Page<>(currentPage,size);
        return RespBean.operationSuccess("查询作品评论成功！", 200, workCommentMapper.selectAllWorkCommentsByPage(page, workComment.getContent()));
    }

    @Override
    public RespBean<Integer> changeWorkCommentById(WorkComment workComment) {
        workComment.setContent(WordFilter.replaceWords(workComment.getContent()));
        if (workCommentMapper.updateById(workComment) == 1){
            return RespBean.operationSuccess("修改评论成功！", 200, 1);
        }
        return RespBean.operationFail("修改评论失败！");
    }

    @Override
    public RespBean<WorkCommentVo> selectCommentById(WorkCommentVo workCommentVo) {
        return RespBean.operationSuccess("查询成功",200,workCommentMapper.selectCommentById(workCommentVo.getCommentId()));
    }

    @Override
    public RespBean<Integer> adminAddComment(WorkComment workComment) {
        workComment.setUserId(userMapper.selectOne(new QueryWrapper<User>().select("user_id").eq("username",workComment.getUsername())).getUserId());
        if (workCommentMapper.insertWorkComment(workComment.getUserId(),WordFilter.replaceWords(workComment.getContent()), workComment.getWorkId()) == 1){
            return RespBean.operationSuccess("添加评论成功", 200, 1);
        }
        return RespBean.operationFail("添加评论失败");
    }

    @Override
    public RespBean<Integer> deleteWorkCommentById(WorkComment workComment) {
        if (workCommentMapper.deleteById(workComment) == 1){
            return RespBean.operationSuccess("删除评论成功！", 200, 1);
        }
        return RespBean.operationFail("删除评论失败！");
    }
}
